<?php

session_start();
require_once ('./Controller/LoginController.php');
require_once ('./Controller/RegController.php');
require_once ('./Controller/NavController.php');
require_once ('./Controller/ArticleController.php');
require_once ('./Controller/ArticleEditorController.php');
require_once ('./View/MasterView.php');
require_once ('./Static/UserStatus.php');
require_once ('./Static/Action.php');

class MasterController{
	/*
	 * Hämtar de olika delarna för att bygga ihop sidan
	 * 
	 * @return string HTML
	 */
	public static function DoControll(){
		$masterView = new MasterView();
		$masterView->AddCSS('css/style.css');
		
		$loginController = new LoginController();
		//sätter html för sidebar
		$loginHTML = $loginController->DoControll();
		
		$navController = new NavController();
		$navController->SetLoginStatus(LoginController::$loginStatus);
		
		//sätter html för meny
		$navHTML = $navController->DoControll();
		
		//kontrollerar och levererar den efterfrågade sidans body
		switch($navController->GetPage()){
			case Action::SHOW_NEWS:
				$articleController = new ArticleController();
				$bodyHTML = $articleController->DoControll(Action::SHOW_NEWS);
				break;
			case Action::SHOW_ARTICLE:
				$bodyHTML = $articleController->DoControll(Action::SHOW_ARTICLE);
				break;
			case Action::USER_REGISTER:
				$regController = new RegController();
				$bodyHTML = $regController->DoControll();
				break;
			case Action::ARTICLE_WRITE:
				if(LoginController::$loginStatus == UserStatus::LOGGED_IN){
					$articleEditorController = new ArticleEditorController();
					ArticleEditorController::$userId = $loginController->GetUserId();
					$bodyHTML = $articleEditorController->DoControll(Action::ARTICLE_WRITE);	
					break;				
				}
				$bodyHTML = $masterView->UserUnauthorisedBox();
				break;
			case Action::ARTICLE_EDIT:
				if(LoginController::$loginStatus == UserStatus::LOGGED_IN){
					$articleEditorController = new ArticleEditorController();
					ArticleEditorController::$userId = $loginController->GetUserId();
					$bodyHTML = $articleEditorController->DoControll(Action::ARTICLE_EDIT);	
					break;				
				}
				$bodyHTML = $masterView->UserUnauthorisedBox();
				break;
			case Action::ARTICLE_LIST:
				if(LoginController::$loginStatus == UserStatus::LOGGED_IN){
					$articleEditorController = new ArticleEditorController();
					ArticleEditorController::$userId = $loginController->GetUserId();
					$bodyHTML = $articleEditorController->DoControll(Action::ARTICLE_LIST);	
					break;				
				}
				$bodyHTML = $masterView->UserUnauthorisedBox();
				break;
			default:
				$articleController = new ArticleController();
				$bodyHTML = $articleController->DoControll(Action::SHOW_NEWS);
				break;
		}
		
		
		return $masterView->GetHTMLPage($navHTML, $bodyHTML, $loginHTML);
		
	}
}
echo MasterController::DoControll();
?>
